package com.cjf.weather.weathercity.service.impl;

import com.cjf.weather.weathercity.service.CityDataService;
import com.cjf.weather.weathercity.util.XmlBuilder;
import com.cjf.weather.weathercity.vo.City;
import com.cjf.weather.weathercity.vo.CityList;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;
import org.springframework.stereotype.Service;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.List;

/**
 * @author cjf
 * @description
 * @create 2019-03-28 23:59
 */
@Service
public class CityDataServiceImpl implements CityDataService {

    @Override
    public List<City> listCity() throws Exception {

        Resource resource = new ClassPathResource("citylist.xml");
        BufferedReader br = new BufferedReader(new InputStreamReader(resource.getInputStream(), "utf-8"));
        StringBuilder stringBuilder = new StringBuilder();
        String str = "";
        while ((str = br.readLine()) != null) {
            stringBuilder.append(str);
        }
        CityList cityList = (CityList) XmlBuilder.xmlStrToOject(CityList.class, stringBuilder.toString());
        if (br != null) {
            br.close();
        }
        return cityList.getCity();
    }

//    public static void main(String[] args) throws Exception {
//        new CityDataServiceImpl().listCity();
//    }

}
